<?php
namespace BoxEdu\Logic;
class UserLogic extends \HomeController
{
    /********用户信息****/
    public function info($user_id)
    {
        $user_info = BoxModel("SystemMember")->load($user_id);    //用户基本信息
        if (!$user_info) return false;
        $user_info['subjectids_info'] = (array)json_decode($user_info['subjectids'], true);
        $subjectModel = BoxModel("edu_subject");
        foreach ($user_info['subjectids_info'] as $k => $one) {
            $subject_info = $subjectModel->where(array('subjectid' => $one['subjectid']))->find();
            $user_info['subjectids_info'][$k]['subject'] = $subject_info['subject'];
            $user_info['subjectids_info'][$k]['picname'] = $subject_info['picname'];
            $user_info['subjectids_info'][$k]['subject_info'] = $subject_info;
        }
        $sync_login = BoxModel('system_sync_login')->where(array('uid' => $user_id))->find();
        return $user_info;
    }


    /******不同的代理码，可能佣金可能不一样**/
    //  public function get_num_money($user_id, $type, $subjectid, $for)
//    {
//        $data = BoxModel("edu_subject")->find($subjectid);
//        $num = $data[$type . '_num_for_' . $for];
//        $num = $num > 0 ? $num : 0;
//        return array('money' => $num);
//    }


    /*******验证优惠券是否可以用***/
    public function check_card($num, $type, $subjectid)
    {
        /*******卡券***/
        if ($type == "card_num") {
            $data = Logic("BoxMall", "Card")->check_card($num);
            return $data;
        }
        if ($type == "school_num") {//使用校友码或代理码
            $data = BoxModel("edu_user")->ByField("school_num", $num);
            //	dump($num);exit();
            if (!$data) return $this->return_array(1, "优惠码不正确");
            $subjectInfo = BoxModel("edu_subject")->where(array('subjectid' => $subjectid))->find();
            //dump($this->user_info);exit();
            $shool_id = $this->user_info['school_id'];//购买者的所在学校
            //线下课程
            $offline = BoxModel("edu_offline_subject_price")->where(array('offline_subject_id' => $subjectInfo['subjectid'], 'school_id' => $shool_id))->find();
            $O = (is_numeric($offline['commission']) && $offline['commission'] > 0) ? $offline['commission'] : 1;//线下推广金额比
            $C = intval($offline['constant']);
            // dump($O);

            $arithmetic = $subjectInfo['school_num_for_buy'];
            if ($arithmetic) {
                $arithmetic = str_replace(array("O", "R", 'C'), array($O, "mt_rand", $C), $arithmetic);
                eval('$num=' . $arithmetic . ';');
            }
            if (I('debug') == 1) {
                dump("学校");
                dump($offline);
                dump("优惠公式：");
                dump($arithmetic);
            }
            $money = array('money' => intval($num));
            //dump($money);exit();
            /*******代理优惠码***/
            return $this->return_array(0, $money);

        }
    }


    /********购买课程***/
    public function buy_subject($data)
    {
        if (!$this->user_id) {
            return $this->return_array(3, "请先登录后再购买");
        }
        $subjectid = $data['subjectid'];
        $offline_subject_ids = $data['offline_subject_ids'];
        $subject = BoxModel("EduSubject")->load($subjectid);
        $user_info = Logic("BoxEdu", "User")->info($this->user_id);
        if (!$subject) {
            return $this->return_array(1, "购买的课程已下架");
        }
        if (strtotime($subject['end_date']) < time()) {
            return $this->return_array(4, "购买的课程已经结课了");
        }
        $order_info = BoxModel("edu_subject_order")->where("user_id='$this->user_id'  and subjectid='$subjectid' and status in (2,4)")->find();
        if ($order_info) {
            return $this->return_array(2, "您已经购买过这课程");
        }
        $this->goods_amount = $subject['price']; //商品价
        /****加上线下课程的价格*****/
        $offline_subject_price = 0;
        // $offline_subject=BoxModel("edu_offline_subject")->get("in ($offline_subject_ids)");
        $offline_subject = Logic("BoxEdu", "Subject")->get_offline_subject_price(array('school_id' => $user_info['school_id'], 'subjectid' => $subjectid));
        if ($offline_subject_ids) {
            if ($offline_subject['price'] > 0) {
                $offline_subject_price = $offline_subject['price'];
                $this->goods_amount += $offline_subject['price'];
            }
        }
        /****开通在线报名*/
        $examination_fee = 0;
        if ($data['examination_id']) {
            if ($offline_subject['examination_fee'] > 0) {
                $examination_fee = $offline_subject['examination_fee'];
                $this->goods_amount += $offline_subject['examination_fee'];
            }
        }
        /**********一元试用**/
        if ($data['type'] == 2) {
            $this->goods_amount = 1;
        }
        $model = BoxModel("system_order");
        $model->startTrans();//开启事务
        // $HomeUrl = BOX_MODULE_NAME == "Wap" ? MEduUrl : EduUrl;
        if ($offline_subject_ids) {//需要在线选座
            $child_subject = BoxModel("edu_subject")->where(array('parent_id' => $subjectid))->find();
            $jump_subject_id = $child_subject['subjectid'] ? $child_subject['subjectid'] : $subjectid;
            $suceess_url = HomeUrl . U('Subject/offline_select_course', array('subjectid' => $jump_subject_id));
        } else {
            $suceess_url = HomeUrl . U('Index/my_subject', array('type' => $this->type));
        }

        $row = array(
            'store_id' => 1,
            'subjectid' => $subjectid,
            'user_id' => $this->user_id,
            'payment_id' => $data['payment_id'],
            'card_num' => $data['card_num'],
            'school_num' => $data['school_num'],
            'use_yie' => $data['use_yie'],
            'agent_num' => $data['agent_num'],
            'order_amount' => $this->goods_amount,
            'goods_amount' => $this->goods_amount,
            'type' => $data['type'],
            'order_type' => 'subject',
            'pid_user_id' => $pid_user_id,
            'suceess_url' => $suceess_url,
            'offline_subject_ids' => $offline_subject_ids,
            'offline_subject_price' => $offline_subject_price,
            'title' => '购买课程，名称：' . $subject['subject'],
            'body' => '购买课程，名称：' . $subject['subject'],
            'log_title' => $subject['subject'],//标题
            'event' => '00018',//积分事件
            'examination_fee' => $examination_fee,
            'examination_id' => $data['examination_id'],//要线报名
            'url' => HomeUrl . U('Index/subject_detail', array('subject_id' => $subject['subjectid'])),
        );
        $r = Logic("BoxMall", "Order")->add($row);
        //	dump($row);exit();
        $order_sn = $r['order_sn'];
        $GLOBALS['pay_order_sn'] = $order_sn;
        if (!$r) {
            $model->rollback();
        } else {
            $model->commit();
            if ($row['order_amount'] > 0) {//在线结算金额如果大于0
                return $this->return_array(0, HomeUrl . "/BoxPay/Order/index/order_sn/" . $order_sn . ".html");
            } else {
                session("pay_verify", true);
                A("BoxPay/Order", "", 1)->succeed($order_sn);
                if ($r) {
                    return $this->return_array(3, $suceess_url);
                } else {
                    return $this->return_array(1, "订单提交失败");
                }
            }
        }
    }


    /*******开通课程*****/
    public function add_subject($user_id, $subject_id, $offline_subject_ids = 1, $type = 1)
    {

        //$user_id = $data['user_id'];
        $user_info = Logic("BoxEdu", "User")->info($user_id);
        $findUser = BoxModel("edu_user")->where(array('user_id' => $user_id))->find();
        if ($findUser) {
            $subjectids = json_decode($findUser['subjectids'], true);
            $classids = json_decode($findUser['classids'], true);
        }
        $subject_find = BoxModel("edu_subject")->where(array('subjectid' => $subject_id))->find();
        $subjectid_data = array(
            'type' => $type,
            'subjectid' => $subject_id,
            'end_date' => $subject_find['end_date'],
            'offline_subject' => $offline_subject_ids,//是否开通线下班级
        );
        $subjectids[$subject_find['subjectid']] = $subjectid_data;
        $classids[$subject_find['subject_class']] = $subject_find['subject_class'];
        $arr = array(
            'subjectids' => json_encode($subjectids),
            'classids' => json_encode($classids),
        );
        if ($findUser) {
            $r = BoxModel("edu_user")->where("user_id='$user_id'")->save($arr);
        } else {
            $arr['user_id'] = $user_id;
            $r = BoxModel("edu_user")->add($arr);
        }
        return $r;

    }

    /***************订单支付成功开始处理这里*******/
    public function buy_subject_sucess($order_sn)
    {


        if (!session("pay_sucess")) {
            return false;
        }
        session("pay_sucess", null);
        $data = BoxModel("edu_subject_order")->where(array('order_sn' => $order_sn))->find();
        if ($data['status'] == 2) {
            return true;
        }
        /*********加入课程*****/
        $this->add_subject($data['user_id'], $data['subjectid'], $data['offline_subject_ids'], $data['type']);

        /*******修改订单状态*****/
        $r = BoxModel("edu_subject_order")->where("order_sn='$order_sn'")->save(array('status' => 2, 'pay_time' => date("Y-m-d H:i:s")));

        /*************在线支付金额支出*****/
        //  $remark = $data['type'] == 2 ? "一元试听" : "开通课程";
        $subject_find = BoxModel("edu_subject")->where(array('subjectid' => $data['subjectid']))->find();
        $num = $data['order_amount'];
        $row = array(
            'user_id' => $data['user_id'],
            'num' => $num,
            'title' => $subject_find['subject'],
            'url' => HomeUrl . U('Index/subject_detail', array('subject_id' => $data['subjectid'])),
            'order_sn' => $order_sn,
            'event' => '00010',
            'subjectid' => $data['subjectid'],
        );
        $r = Logic('BoxSns', 'Log')->add($row);
        /*******加到财务统计******/
        $row['type'] = "subject";
        hook("Finance/Index/add", $row);
        /****************试看就不往下执行了*********************/
        if ($data['type'] == 2) {
            return $r; //
        }
        /***************购买课程获得学币***********************/
        $num = $subject_find['tujibi'];
        $row = array(
            'user_id' => $data['user_id'],
            'num' => $num,
            'title' => $subject_find['subject'],
            'order_sn' => $order_sn,
            'title' => $subject_find['subject'],
            'event' => '00012',
            'url' => HomeUrl . U('Index/subject_detail', array('subject_id' => $data['subjectid'])),
            'subjectid' => $data['subjectid'],
        );
        //dump($row);
        $r = Logic('BoxSns', 'Log')->add($row);

        /******购买课程获得提问数*****************************/
        $num = $subject_find['ask_num'];
        $row = array(
            'user_id' => $data['user_id'],
            'num' => $num,
            'order_sn' => $order_sn,
            'title' => $subject_find['subject'],
            'url' => HomeUrl . U('Index/subject_detail', array('subject_id' => $data['subjectid'])),
            'subjectid' => $data['subjectid'],
            'event' => '00013',
        );
        //dump($row);
        $r = Logic('BoxSns', 'Log')->add($row);

        /*********************推广代理获得佣金*********************/
        $this->arithmetic($order_sn);
        //$school_find = BoxModel("edu_user")->where(array('school_num'=>$data['num']))->find();
        $this->beSchool($data['user_id']); //获得校友推广资格
        /***************************处理完********************************/
        //可领取金额
        BoxModel("edu_red_packets")->add(array(
            'user_id' => $data['user_id'],
            'num' => intval(dict('school_frozen_capital')),
            'status' => 1,
            'title' => "开通课程：" . $subject_find['subject'],
            'add_time' => time(),
        ));

        //开通课程可领取金额通知
        $params = array(
            'user_id' => $data['user_id'],
            'num' => intval(dict('school_frozen_capital')),
            'title' => $subject_find['subject'],
            'url' => HomeUrl . '/Index/my_subject',
        );
        Logic("BoxSns", "Noti")->send("00001", $params);
        return $r;
    }

    public function arithmetic($order_sn, $dubeg = 0)
    {//代理佣金
        header("Content-type: text/html; charset=utf-8");
        $data = BoxModel("edu_subject_order")->where(array('order_sn' => $order_sn))->find();
        $order_user = BoxModel("system_member")->where(array('user_id' => $data['user_id']))->find();
        $subject_find = BoxModel("edu_subject")->where(array('subjectid' => $data['subjectid']))->find();
        $num = $data['school_num'];
        if (!$num) {
            return;
        }
        $school_find = BoxModel("edu_user")->where(array('school_num' => $num))->find();
        $user_info = BoxModel("edu_students")->where(array('user_id' => $data['user_id'], 'is_show' => 1))->find();
        $shool_id = $user_info['school_id'];//购买者的所在学校
        //线下课程
        $offline = BoxModel("edu_offline_subject_price")->where(array('offline_subject_id' => $subject_find['subjectid'], 'school_id' => $shool_id))->find();
        $O = (is_numeric($offline['commission']) && $offline['commission'] > 0) ? $offline['commission'] : 1;//线下推广金额比例
        $C = intval($offline['constant']);

        $A = 0;
        $B = 0;
        if ($school_find) {
            //是校园合伙人
            if ($school_find['is_agent'] == 1) { //如果直接使用代理码那么就没B
                $B = 0;
                $A = 1;
                $a_user_id = $school_find['user_id'];
            }
            if ($school_find['is_agent'] == 0 && $school_find['agent_pid'] > 0) {//如果直接使用校友码有代理
                $A = 1;
                $B = 1;
                $a_user_id = $school_find['agent_pid'];
                $b_user_id = $school_find['user_id'];
            }
            if ($school_find['is_agent'] == 0 && $school_find['agent_pid'] == 0) {//如果直接使用校友码,没代理
                $A = 0;
                $B = 1;
                $b_user_id = $school_find['user_id'];
            }
        }
        $a_arithmetic = $subject_find['agent_num_for_seller'];//是校园合伙人A算法
        $b_arithmetic = $subject_find['school_num_for_seller'];//是B推广的算法
        $a_arithmetic = str_replace(array("O", "P", "R", "C"), array($O, $B, "mt_rand", $C), $a_arithmetic);
        $b_arithmetic = str_replace(array("O", "P", "R", "C"), array($O, $A, "mt_rand", $C), $b_arithmetic);
        if ($dubeg) {
            dump(array('school_num' => $num));
            dump("优惠码" . $data['school_num']);
            echo "优惠码信息";
            dump($school_find);
            echo "A算法";
            dump($a_arithmetic);
            echo "B算法";
            dump($b_arithmetic);
            dump("B:$B");
            dump("A:$A");
            echo "课程信息";
            dump($subject_find);
        }

        if ($B && $subject_find['school_num_for_seller']) {
            eval('$num=' . $b_arithmetic . ';');
            $row = array(
                'user_id' => $b_user_id,
                'num' => intval($num),
                'order_sn' => $order_sn,
                'order_nickname' => $order_user['nickname'],
                'order_user_id' => $order_user['user_id'],
                'title' => $subject_find['subject'],
                'url' => HomeUrl . U('Index/subject_detail', array('subject_id' => $data['subjectid'])),
                'subjectid' => $data['subjectid'],
                'event' => '00008',
            );
            if ($dubeg) {
                echo "B推广佣金";
                dump($row);
            } else {
                $r = Logic('BoxSns', 'Log')->add($row);
            }
        }
        //如果是校友推广者，学校代理也要获得佣金
        if ($A && $subject_find['agent_num_for_seller']) {
            $agent_find = BoxModel("edu_user")->where(array('user_id' => $a_user_id))->find();
            $agent_apply = BoxModel("edu_user_agent_apply")->where(array('user_id' => $a_user_id))->find();
            eval('$num=' . $a_arithmetic . ';');
            $row = array(
                'user_id' => $a_user_id,
                'num' => intval($num),
                'order_sn' => $order_sn,
                'title' => $subject_find['subject'],
                'order_nickname' => $order_user['nickname'],
                'order_user_id' => $order_user['user_id'],
                'url' => HomeUrl . U('Index/subject_detail', array('subject_id' => $data['subjectid'])),
                'subjectid' => $data['subjectid'],
                'event' => '00008',
            );
            //dump($row);exit();
            if ($dubeg) {
                echo "A代理佣金";
                dump($row);
            } elseif ($agent_find['is_agent'] == 1 && $agent_apply['status'] == 2 && $agent_apply['expirationTime'] > time()) {
                $r = Logic('BoxSns', 'Log')->add($row);
            }
            //$agent_pid=$agent_find['user_id'];
        }
        $user = BoxModel("edu_user")->where(array('user_id' => $data['user_id']))->find();
        if ($dubeg) {
            echo "用户信息";
            dump($user);
        }
        if ($user['is_agent'] == 0 && $user['school_pid'] == 0) {
            $row = array(
                'school_pid' => $school_find['user_id'],
                'agent_pid' => $school_find['user_id'],
            );
            if ($dubeg) {
                echo "用户代理更新";
                dump($row);
            } else {
                BoxModel("edu_user")->where(array('user_id' => $data['user_id']))->save($row);
            }
        }


    }


    /********学习进度*******/
    public function learning_pre($subjectid)
    {
        //  $exams_count = BoxModel("edu_exams")->where("subjectid='$subjectid'")->count();
        //$exams_count_has = BoxModel("edu_examhistory")->where("subjectid='$subjectid' and user_id='$this->user_id'")->count(); //已经做试卷
        $count_has = BoxModel("edu_knows")->where(array('subjectid' => $subjectid))->count();
        $done_count = BoxModel("edu_knows_user")->where(array('subjectid' => $subjectid, 'type' => 2, 'user_id' => $this->user_id))->count();
        $per = round(($done_count / $count_has) * 100, 2);
        return $per . "%";
    }

    /*********获得经验值******/
    public function add_xuebazi($row)
    {
        $row['user_id'] = $this->user_id;
        $r = Logic('BoxSns', 'Log')->add($row);
        $subjectid = $row['subjectid'];
        /******更新课程排行名****/
        if ($r['error'] == 0 && $subjectid) {
            $data = BoxModel("edu_subject")->where(array('subjectid' => $subjectid))->find();
            $subjectid = $data['parent_id'] ? $data['parent_id'] : $subjectid;
            $where = array(
                'subjectid' => $data['subjectid'],
                'user_id' => $this->user_id,
            );
            $rank = BoxModel("edu_subject_rank")->where($where)->find();
            $num = $rank['num'] ? $rank['num'] : 0;
            $row2 = array(
                'subjectid' => $data['subjectid'],
                'subject_class' => $data['subject_class'],
                'user_id' => $this->user_id,
                'num' => $num + $row['num'],
            );
            if ($rank) {
                BoxModel("edu_subject_rank")->where(array('id' => $rank['id']))->save($row2);
            } else {
                BoxModel("edu_subject_rank")->add($row2);
            }
        }
        return $r;
    }

    /********学员心声****/
    public function add_comment_subject_order($row)
    {
        $subjectid = $row['subjectid'];
        $data = BoxModel("EduSubjectOrder")->where("user_id='$this->user_id' and subjectid='$subjectid'")->find();
        if (!$data) {
            return $this->return_array("1", "开通课程之后才能参与评论");
        }
        if ($data['type'] == 2) {
            return $this->return_array("1", "你购买的为试听课程，不能参与评论");
        }
        if ($data['status'] == 4) {
            return $this->return_array("1", "已经评论过了");
        }

        $arr = array(
            'content' => $row['content'],//必填
            'pid' => $data['order_sn'],
            'type' => 'subject_order',//
            'subjectid' => $row['subjectid'],
        );
        $r = Logic('BoxSns', 'Comment')->add($arr);
        BoxModel("EduSubjectOrder")->where("user_id='$this->user_id' and subjectid='$subjectid'")->save(array('status' => 4));
        if ($r) {
            return $this->return_array("0", "评论成功");
        }

    }

    /********购买答题数***/
    public function buy_ask_num($row)
    {
        $this->payment_id = $row['payment_id'];
        $this->num = $row['num'];
        $goods_amount = $row['goods_amount'];
        $this->yie = is_numeric($row['yie']) ? $row['yie'] : 0;
        // $this->order_amount = $goods_amount;
        /*******对单对应的支付表******/
        $modelPay = BoxModel("edu_ask_order");
        $modelPay->startTrans();//开启事务
        //dump($row);exit();
        $row = array(
            'user_id' => $this->user_id,
            'num' => $this->num,
            //  'payment_id' => $this->payment_id,
            'order_amount' => $goods_amount,
            'goods_amount' => $goods_amount,
            'use_yie' => $this->yie,
            'order_type' => 'ask',
            'suceess_url' => HomeUrl . U('User/ask_num'),
            'title' => '购买提问数' . $this->num . "次",
            'body' => '购买提问数' . $this->num . "次",
            'log_title' => '提问数' . $this->num . "次",//标题
            'event' => '00018',//积分事件
        );
        $r = Logic("BoxMall", "Order")->add($row);
        $order_sn = $r['order_sn'];
        if (!$order_sn) {
            $modelPay->rollback();
            return $this->return_array("1", "数据保存失败");
        } elseif ($row['order_amount'] > 0) {
            $modelPay->commit();
            return $this->return_array("2", HomeUrl . "/BoxPay/Order/index/order_sn/" . $order_sn . ".html");
        } else {
            session("pay_sucess", true);
            $modelPay->commit();
            $this->buy_ask_sucess($order_sn);

            return $this->return_array("0", HomeUrl . "/User/ask_num/");
        }
    }

    /****************购买答题数成功回调****/
    function buy_ask_sucess($order_sn)
    {
        if (!session("pay_sucess")) {
            return false;
        }
        session("pay_sucess", null);
        $data = BoxModel("edu_ask_order")->where(array('order_sn' => $order_sn))->find();
        if ($data['status'] != 1) {
            return true;
        }
        /*******修改订单状态*****/
        $r = BoxModel("edu_ask_order")->where("order_sn='$order_sn'")->save(array('status' => 2, 'pay_time' => date("Y-m-d H:i:s")));
        /******购买课程获得提问数*****************************/
        $num = $data['num'];
        $row = array(
            'user_id' => $data['user_id'],
            'num' => $num,
            'order_sn' => $order_sn,
            'event' => "00014",
            'subjectid' => 0,
        );
        $r = Logic('BoxSns', 'Log')->add($row);

        /*************金额支出*****/
        $num = $data['order_amount'];
        $row = array(
            'user_id' => $data['user_id'],
            'num' => $num,
            'aks_num' => $data['num'],
            'order_sn' => $order_sn,
            'event' => "00016",
            'subjectid' => 0,
        );
        if ($num > 0) {
            $r = Logic('BoxSns', 'Log')->add($row);
            /*******加到财务统计******/
            $row['type'] = "ask";
            hook("Finance/Index/add", $row);
        }
        return $r;

    }

    /******添学生**/
    function bing_student($row, $from)
    {

        $row['type'] = 'students';
        $row['user_id'] = $row['user_id'] ? $row['user_id'] : $this->user_id;
        $user_id = BoxModel("system_member")->create_member($row);
        $stu = BoxModel("EduStudents")->where(array('user_id' => $this->user_id))->find();
        if ($stu) {
            return array('error' => 0, 'id' => $stu['id'], 'user_id' => $this->user_id);
        } else {
            return array('error' => 1, 'msg' => '信息保存失败');
        }
    }

    /*****绑定学生信息***/
    function bing_student_old($row, $type = "qq")
    {
        $v = $row[$type];
        $r = BoxModel("edu_students")->where("$type='$v'")->find();
        if ($r) {
            $user_id = $r['user_id'];
            $memberdata = BoxModel("system_member")->load($this->user_id);
            $has_bing = BoxModel("system_sync_login")->where("uid='$user_id'")->find();
            if ($has_bing) {
                return array('msg' => 'QQ帐号已使用' . $has_bing['type'] . '绑定，请使用' . $has_bing['type'] . '帐号登录,如需申诉请联系我们');
            }
            $student_member = BoxModel("system_member")->where("user_id='$user_id'")->find();//导入的用户
            if ($student_member['user_id']) {
                BoxModel("system_member")->where("user_id='$this->user_id'")->delete();
                BoxModel("system_member_statistics")->where("user_id='$this->user_id'")->delete();
            } else {
                $user_id = $this->user_id;
            }
            BoxModel("system_sync_login")->where("uid='$this->user_id'")->save(array('uid' => $user_id));
            Logic("BoxSns", "member")->saveLogin($user_id);//重新登录
            $row['id'] = $r['id'];//学生id
            $row['user_id'] = $r['user_id'];
        } else {
            $row['user_id'] = $this->user_id;
        }
        return $this->add_student($row);//绑定学生信息
    }


    /*****用户完整性***/
    function completion_member($user_id)
    {
        $edu_students = BoxModel("edu_students")->where("user_id='$user_id'")->find();
        $memeber = BoxModel("system_member")->where("user_id='$user_id'")->find();
        $edu_user = BoxModel("edu_user")->where("user_id='$user_id'")->find();
        if (!$memeber) {
            BoxModel("system_member")->add(array('user_id' => $user_id, 'user_name' => $edu_students['qq']));
            BoxModel("system_member_statistics")->add(array('user_id' => $user_id));
        }
        if (!$edu_user) {
            $school_num = createCardNo($user_id);
            BoxModel("edu_user")->add(array('user_id' => $user_id, 'school_num' => $school_num));
        }
    }

    function visit($user_id)
    {
        $where = array(
            'user_id' => $user_id,
            'from_user_id' => $this->user_id,
        );
        $where2 = array(
            'user_id' => $this->user_id,
            'from_user_id' => $user_id,
        );
        $this->add_visit($where, 2);
        $this->add_visit($where2, 1);
    }

    function add_visit($where, $type)
    {
        if ($where['user_id'] && $where['from_user_id'] && $where['user_id'] != $where['from_user_id']) {
            $r = BoxModel("edu_user_visit")->where($where)->find();
            if ($r) {
                $row = array(
                    'updatetime' => time(),
                    'number' => $r['number'] + 1,
                );
                BoxModel("edu_user_visit")->where($where)->save($row);
            } else {
                $where['updatetime'] = time();
                $where['type'] = $type;
                BoxModel("edu_user_visit")->add($where);
            }
        }
    }


    //收藏一道题
    function collect_questions($param)
    {
        if ($exams_id = $param['exams_id']) {
            $data = BoxModel("edu_exams")->where(array('examid' => $exams_id))->find();
            $param['know_id'] = $data['knowsid'];
            $param['subject_id'] = $data['subjectid'];
            $param['sections_id'] = $data['sectionid'];
        }
        $param['user_id'] = $this->user_id;
        $param['addtime'] = date("Y-m-d H:i:s");
        $find = BoxModel("edu_questions_user")->where(array('questions_id' => $param['questions_id'], 'type' => $param['type'], 'user_id' => $this->user_id))->find();
        if ($find) {
            BoxModel("edu_questions_user")->where(array('id' => $find['id']))->delete();
        }
        $r = BoxModel("edu_questions_user")->add($param);
        return $r;
    }

    //成为校友
    function beSchool($user_id)
    {
        $school_num = createCardNo($user_id);
        $data = BoxModel("edu_user")->where(array('user_id' => $user_id))->find();
        $row = array(
            //'frozen_capital'=>intval(dict('school_frozen_capital')),
            'school_num' => $school_num,//学校代理
        );
        if (!$data['school_num']) {
            BoxModel("edu_user")->where(array('user_id' => $user_id))->save($row);
        }


    }

    //成为代理
    function beAgent($user_id, $agent_frozen_capital)
    {
        $agent_frozen_capital = $agent_frozen_capital ? $agent_frozen_capital : intval(dict('agent_frozen_capital'));
        $school_num = createCardNo($user_id);
        $data = BoxModel("edu_user")->where(array('user_id' => $user_id))->find();
        $row = array(
            'is_agent' => 1,
        );
        if (!$data['school_num']) {
            $row['school_num'] = $school_num;
        }
        if ($data['is_agent'] != 1) {
            BoxModel("edu_user")->where(array('user_id' => $user_id))->save($row);
            //成为校园代理
            BoxModel("edu_red_packets")->add(array(
                'user_id' => $user_id,
                'num' => $agent_frozen_capital,
                'status' => 1,
                'title' => "成为校园合伙人",
                'add_time' => time(),
            ));
            //成为突击教育的代理信息通知
            $data = BoxModel("edu_user_agent_apply")->where(array('user_id' => $user_id))->find();
            $params = array(
                'user_id' => $user_id,
                'num' => $agent_frozen_capital,
                'url' => HomeUrl . "/User/apply_agent",
                'expirationTime' => date("Y-m-d", $data['expirationTime']),
            );
            Logic("BoxSns", "Noti")->send("00002", $params);
        }
    }

    //获得推广者ID
    function getPid($pid)
    {
        $return = array(
            'agent_pid' => 0,
            'school_pid' => 0,
        );
        if (intval($pid) > 0) {//是数字查id
            $where = array(
                'user_id' => $pid,
            );
        } elseif (is_string($pid)) {//使优惠码
            $where = array(
                'school_num' => $pid,
            );
        } else {//默认返回
            return $return;
        }
        $data = BoxModel("edu_user")->where($where)->find();
        if ($data) {
            $return['agent_pid'] = $data['agent_pid'];
            $return['school_pid'] = $data['school_pid'];
            if ($data['is_agent']) {//如果是代理
                $return['agent_pid'] = $data['user_id'];
            }
        }
        return $return;
    }


}